package cn.com.ethos.search.query;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.json.JSONStringer;

public class ResultResponse extends Response{
	
	List<Document> list;
	int total;
	
	public List<Document> getList() {
		return this.list;
	}
	
	public void setList(List<Document> list) {
		this.list = list;
	}
	
	public int getTotal() {
		return this.total;
	}
	
	public void setTotal(int total) {
		this.total = total;
	}
	
	public ResultResponse(List<Document> list, int total) {
		this.list = list;
		this.total = total;
	}
	
	public ResultResponse() {
		this.list = new ArrayList<Document>();	
	}
	
	public void addDocument(Document doc) {
		this.list.add(doc);
	}
	
	public String toString() {
		return this.list.toString();
	}
	
	public String getBody() {
		JSONStringer json = new JSONStringer();
		try {
			json.object()
				.key("total").value(this.total)
				.key("list").array();
			for(Document d : this.list) {
				json.object();
				for(Object o : d.getFields()) {
					Field f = (Field)o;
					json.key(f.name()).value(f.stringValue());
				}
				json.endObject();
			}
			json.endArray()
				.endObject();
			
			return json.toString();
			
		} catch(Exception ex) {
			return this.getSimpleExceptionBody(ex);
		}
	}

}
 